<?php
$pageArray = '';
function getCatName($catID)
{
	$tables = array();
	$tables[0] = 'inv_category';
	
	$fields = array();
	$fields[0] = 'catName';
	
	$whereClause = 'catID="'.$catID.'"';
	
	$data = SQLRecover($tables, $fields, $whereClause);
	
	return $data['catName'][0];
}
function getDeletedCatID()
{
	$tables = array();
	$tables[0] = 'inv_category';
	
	$fields = array();
	$fields[0] = 'catID';
	
	$whereClause = 'catName = "Deleted"';
	
	$data = SQLRecover($tables, $fields, $whereClause);
	
	return $data['catID'][0];
}
/* -----------LOGIN PAGE---------------- */
function displayLogin()
{	
	$logBoxes = '';
	if(installation() == 'create' || get('forceInstall') == AUTH)
	{
		$logBoxes = '<input id="fname" title="First Name" type="text" name="fname" class="loginTxt"/><br />
					<input id="lname" title="Last Name" type="text" name="lname" class="loginTxt"/><br />
					<input id="email" title="Email" type="text" name="email" class="loginTxt"/><br />';
	}
	$logText = (installation() == 'create' ? 'Create User' : 'Secure Login');
	if(get('forceInstall') != 'null')
	{
		$log = 'log=create&forceInstall='.AUTH;
	}
	else
	{
		$log = 'log='.installation();
	}
	$disp = '
		<div class="grid_12">
			<div id="mainHeaderNoLogin" class="centerAlign">
				<img src="./img/home/login.png" /><br />
				<h1>'.$logText.'</h1>
				<form method="POST" action="'.INDEX.'home'.EXT.$log.'">
					'.$logBoxes.'
					<input id="username" title="Your Username" type="text" name="userName" class="loginTxt"/><br />
					<input id="password" title="Your Password" type="password" name="password" class="loginTxt"/><br />
					<input type="submit" class="loginSubmit" value="Sign In" />
				</form>
			</div>
		</div>
	';
	return $disp;
}
/* ---------- */

/* ------------------HOME PAGE--------------- */
function displayAdmin()
{
	$disp = '
	<div id="mainDiv">
	<h1>Inventory - '.getdatenow().'</h1>
		<div id="mainInventory">';
	$disp2 = getTable();	
	$disp2 .= '</div>
	
		<div id="inventoryControls" class="centerAlign">
			<input type="button" id="but1" value="General Count" class="buttons" rel="'.INDEX.'home"/>
			<input type="button" id="but2" value="Current Activity" class="buttons" rel="'.INDEX.'home'.EXT.'but=2"/>
			<input type="button" id="but3" value="Transaction View" class="buttons" rel="'.INDEX.'home'.EXT.'but=3"/>
		</div>
	</div>
	';
	return $disp.$disp2;
}
/* ----------- */

function displayPage()
{
	return adminContent(displayAdmin(),displayLogin());
}

function getTable()
{
	$but = get('but');
	if($but == 2) {
		//ACTION VIEW -- CHANGE TO LOCATION VIEW
		$tables = array();
		$tables[0] = 'qtyIndex';
		
		$fields = array();
		$fields[0] = '';
		
		$whereClause = 'checked = "0"';
		
		$data = SQLRecover($tables, $fields, $whereClause);
	}
	
	elseif($but == 3)
	{
		//TRANSACTION VIEW
		$display = '';
		$tables = array();
		$fields = array();
		$tables[0] = 'inv_orders';
		$fields[0] = 'timestamp';
		$fields[1] = 'status';
		$where = 'none';
		$sql = SQLRecoverOrder($tables, $fields, $where, "timestamp", "DESC");
		$count = SQLCount($tables, $where);
		$ncount = $count['COUNT(*)'][0];
		for($i = 0; $i < $ncount; $i++)
		{
			if($sql['status'][$i] == 1)
				$status = 'purchase';
			$display .= $sql['timestamp'][$i];
			$display .= ' ';
			$display .= $status;
			$display .= '<br>';
		} 
	}
	else
	{
		$tables = array();
		$tables[0] = 'inv_item';
		
		$fields = array();
		$fields[0] = 'itemID';
		$fields[1] = 'category';
		$fields[2] = 'itemDesc';
		$whereClause = 'active < 1 AND category != "'.getDeletedCatID().'" ORDER BY category ASC';
		
		$data = SQLDistinct($tables, $fields, $whereClause);
		
		$display = '<table id="invTableMain" class="inventoryTable">
		<th class="inventoryView">
		Item ID
		</th>
		<th class="inventoryView">
		Description
		</th>
		<th class="inventoryView">
		Category
		</th>
		<th class="inventoryView qty">
		Quantity
		</th>
		<th class="inventoryView">
		Locations
		</th>
		';
		for ($i=0; $i<sizeOf($data['itemID']); $i++)
		{
			$display .= '<tr><td class="itemID">'.$data['itemID'][$i].'</td>
			';
			$display .= '<td class="itemDesc">'.substr($data['itemDesc'][$i], 0 , 34).'</td>
			';
			$display .= '<td class="category">'.getCatName($data['category'][$i]).'</td>
			';
			
			$tables = array();
			$tables[0] = 'inv_qtyIndex';
			
			$fields = array();
			$fields[0] = 'locName';
			$fields[1] = 'quantity qty';
			
			$whereClause = 'itemID="'.$data['itemID'][$i].'" AND checked="0"';
			
			$data2 = SQLRecover($tables, $fields, $whereClause);
			
			if($data2['queryStatus'] == 'DB Query success!')
			{
				$qtyTotal = 0;
				$locationList = '';
				for ($x=0; $x<sizeOf($data2['locName']); $x++)
				{
						$qtyTotal += $data2['qty'][$x];
						$locationList .= $data2['locName'][$x].' '; 
				}
				$display .= '<td class="qty">'.$qtyTotal.'</td><td class="location">'.$locationList.'</td>';
			}
			
			else
			{
				$display .= '<td class="qty">0</td><td class="location">N/A</td>
				';
			}
			$display .= '</tr>';
		}
		$display .= '</table>';
	}
	return $display;
}
?>